package com.huiztech.kaoqinji;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;

//
///**
// * 继承MapView重写onTouchEvent实现地图选点
// *
// * @author zhiyuan
// *
// */
public class MyLocationMapView extends MapView {
    static PopupOverlay pop = null;// 弹出泡泡图层，点击图标使用
    Context context;
     GeoPoint pt;

    public MyLocationMapView(Context context) {
        super(context);
        this.context = context;
    }

    public MyLocationMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLocationMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        //将像素坐标转为地址坐标
        pt = this.getProjection().fromPixels(x, y);
        setAddressOverlay(pt);
        if (!super.onTouchEvent(event)) {
            // 消隐泡泡
            if (pop != null && event.getAction() == MotionEvent.ACTION_UP) pop.hidePop();
        }
        //return super.onTouchEvent(event);
        return true;
    }

    public void setAddressOverlay(GeoPoint pt) {
        ItemizedOverlay<OverlayItem> itemOverlay = new ItemizedOverlay<OverlayItem>(null, this);
        //生成Item
        OverlayItem item = new OverlayItem(pt, "", null);
        //得到需要标在地图上的资源
        Drawable marker = getResources().getDrawable(R.drawable.icon_gcoding);
        //为maker定义位置和边界
        marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
        //给item设置marker
        item.setMarker(marker);
        //在图层上添加item
        itemOverlay.addItem(item);
        //清除地图其他图层
        this.getOverlays().clear();
        //添加一个标注ItemizedOverlay图层
        this.getOverlays().add(itemOverlay);
        //执行刷新使生效
        this.refresh();
    }
}